#include "newcalendar.h"
#include "ui_newcalendar.h"

newCalendar::newCalendar(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::newCalendar)
{
    ui->setupUi(this);
    // 将控件指针放入list中
    putDayExInList();
    initCalendarProperty();// 初始化每个日期都没有被选中，index为响应的index
    setCurYearMonth(1, 1, 1);
    connectMouseClickSlot();// 给每一天连接槽函数

    QFile styleFile(":/img/style.qss");
    styleFile.open(QIODevice::ReadOnly);
    QString strStyle(styleFile.readAll());;
    setStyleSheet(strStyle);
}

newCalendar::~newCalendar()
{
    delete ui;
}

void newCalendar::putDayExInList()
{
     listDayWidget = this->findChildren<DayWidget *>();
//    QList<DayEx *> ds = this->findChildren<DayEx *>();
//    int length = ds.length();
//    for(int i = 0; i < length; i ++)
//    {
//        listDayWidget.append(ds[i]);
//    }
}


void newCalendar::initCalendarProperty()
{
    int nCnt = listDayWidget.length();
    for(int i = 0; i < nCnt; i ++)
    {
        listDayWidget[i]->setSelected(false);
        listDayWidget[i]->setIndex(i);
    }
}

void newCalendar::setCurrentDateTime(int nYear, int nMonth, int nDay)
{
    setCurYearMonth(nYear, nMonth, nDay);
    initCalendarProperty();
    QDate d;
    QDate dCurMonthFirstDay(nYear, nMonth, 1);
    // 计算这个月一号是星期几以及这个月有多少天
    int nDayOfWeek = dg.getDayOfWeek(nYear, nMonth, 1);
    int nDaysOfMonth = dg.getDaysOfMonth(nYear, nMonth);
    if(nDayOfWeek == 0)
        nDayOfWeek = 7;
    // 恢复颜色
     int nCnt = listDayWidget.length();
    for(int i = 0; i < nCnt; i ++)
    {
        listDayWidget[i]->setSelected(false);
    }
    // 填充当前月一号之前的日期控件
    for(int i = 0; i < nDayOfWeek; i ++)
    {
        QDate dTemp = dCurMonthFirstDay.addDays(-1*(nDayOfWeek - i));
        listDayWidget[i]->setDay(dTemp.day());
        listDayWidget[i]->setMonType(-1);
        listDayWidget[i]->setSelected(false);
        listDayWidget[i]->setHint(QString("上个月"));
    }
    // 填充这个月的
    for(int i = 0; i < nDaysOfMonth; i ++)
    {
        listDayWidget[i+nDayOfWeek]->setDay(i+1);
        listDayWidget[i+nDayOfWeek]->setMonType(0);
        listDayWidget[i+nDayOfWeek]->setSelected(false);
        listDayWidget[i+nDayOfWeek]->setHint(QString("未选择"));
    }
    // 填充下个月的日期控件
    QDate dNextMonthFirstDay = dCurMonthFirstDay.addMonths(1);
    for(int i = nDayOfWeek + nDaysOfMonth; i < nCnt; i ++)
    {
        listDayWidget[i]->setDay(i-nDayOfWeek - nDaysOfMonth + 1);
        listDayWidget[i]->setMonType(1);
        listDayWidget[i]->setSelected(false);
        listDayWidget[i]->setHint(QString("下个月"));
    }

    // 设置上方的标题
    setYearMonthLabelHint();
    // 设置左边
    setCalendarHint();
    updateCtrlStyle();
}

void newCalendar::setLabText(QLabel *pLabel, const QString &strText)
{
    pLabel->setText(strText);
}

void newCalendar::setYearMonthLabelHint()
{
    setLabText(ui->lab_YearMonthHint, QString("%1 年 %2 月").arg(nCurYear).arg(nCurMonth));
}

void newCalendar::setCurYearMonth(int nYear, int nMonth, int nDay)
{
    nCurMonth = nMonth;
    nCurYear = nYear;
    nCurDay = nDay;
}

void newCalendar::on_btn_NextMonth_clicked()
{
    QDate d(nCurYear, nCurMonth, 1);
    QDate dTemp = d.addMonths(1);
    setCurrentDateTime(dTemp.year(), dTemp.month());
}

void newCalendar::on_btn_LastMonth_clicked()
{
    QDate d(nCurYear, nCurMonth, 1);
    QDate dTemp = d.addMonths(-1);
    setCurrentDateTime(dTemp.year(), dTemp.month());
}


void newCalendar::connectMouseClickSlot()
{
    int nCnt = listDayWidget.length();
    for(int i = 0; i < nCnt; i ++)
    {
        connect(listDayWidget[i], SIGNAL(signalMouseDoubleClick(bool,int,int)), this, SLOT(slotMouseClickDay(bool,int,int)));
    }
}


void newCalendar::slotMouseClickDay(bool bSelected, int nIndex, int nMonth)
{
    if(nMonth == -1 || nMonth == 1) // 上一个月或者下一个月
    {
        QDate d(nCurYear, nCurMonth, 1);
        QDate dTemp = d.addMonths(nMonth);
        nCurYear = dTemp.year();
        nCurMonth = dTemp.month();
        nCurDay = listDayWidget[nIndex]->getDay();
        setCurrentDateTime(nCurYear, nCurMonth, nCurDay);
    }
    else // 选中其中的某一天
    {
        nCurDay = listDayWidget[nIndex]->getDay();
        setDaySelectStatus(nCurDay, !listDayWidget[nIndex]->getSelected());
        QString strText =((!bSelected) == true ? "选中" : "未选择" );
        listDayWidget[nIndex]->setHint(strText);
        // 发送选中的信号
        emit signalSelectDay(listDayWidget[nIndex]->getSelected(), nCurYear, nCurMonth, nCurDay);
    }
    setCalendarHint();
    updateCtrlStyle();
}


void newCalendar::setCalendarHint()
{
    ui->lab_YearMonthDay->setText(QString("%1 年 %2月 %3日").arg(nCurYear).arg(nCurMonth).arg(nCurDay));
    ui->lab_DayOfWeek->setText(getDayOfWeek());
    ui->lab_DayOfMonth->setText(QString("%1").arg(nCurDay));
}

QString newCalendar::getDayOfWeek()
{

    switch (dg.getDayOfWeek(nCurYear, nCurMonth, nCurDay)) {
    case 0:
        return "周日";
        break;
    case 1:
        return "周一";
        break;
    case 2:
        return "周二";
        break;
    case 3:
        return "周三";
        break;
    case 4:
        return "周四";
        break;
    case 5:
        return "周五";
        break;
    case 6:
        return "周六";
        break;
    default:
        break;
    }
}

void newCalendar::paintEvent(QPaintEvent *)
{
     QStyleOption opt;
     opt.init(this);
     QPainter p(this);
     style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}

// 选中这个月的某个状态
void newCalendar::setDaySelectStatus(int day, bool bSelect,const QString& strText)
{
    int nCnt = listDayWidget.length();
    for(int i = 0; i < nCnt; i ++)
    {
        if(listDayWidget[i]->getDay() == day && listDayWidget[i]->getMonType() == 0)
        {
            listDayWidget[i]->setSelected(bSelect);
            listDayWidget[i]->setHint(strText);
        }
    }
}

void newCalendar::updateCtrlStyle()
{
    int length = listDayWidget.length();
    for(int i = 0; i < length; i ++)
    {
        style()->unpolish(listDayWidget[i]);
        style()->polish(listDayWidget[i]);
    }
    update();
}

